//
//  CZNewFeatureViewController.swift
//  1215微博终极项目
//
//  Created by chok崋 on 15/12/17.
//  Copyright © 2015年 周卓华. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CZNewFeatureViewController: UICollectionViewController {
    //有多少个界面

    //声明布局
private let layout = UICollectionViewFlowLayout()
 private let count  = 4
    //重写初始化的方法
    //appdelegate会调用没有参数的方法
     init()
    {
      super.init(collectionViewLayout: layout)
    }

     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       
        prepareCollectionView()
        
            }

    //准备collectionView的UI准备
    private func prepareCollectionView()
    {
        //注册cell
        self.collectionView!.registerClass(CZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
         layout.itemSize = UIScreen.mainScreen().bounds.size
        
         //取消间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //水平滑动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //分页显示
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
    }
   

    


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        
        return count
    }

    //设置cell的内容
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        //注册cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CZNewFeatureCell
    
        //cell.backgroundColor = UIColor.brownColor()
        cell.index = indexPath.item
    
        return cell
    }
    
    //显示完cell的时候调用方法
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获得当前显示的页面
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        //判断当前的页面时候最后一页
        if visibleIndexPath.item == count - 1
        {
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! CZNewFeatureCell
            
            cell.startAnimation()
        }
    }
    

   

}

//cell的类
class CZNewFeatureCell : UICollectionViewCell
{
    //当index的值发生变化时
    var index = 0 {
     
        didSet{
            backGroundImage.image = UIImage(named: "new_feature_\(index+1)")
           
            btn.hidden = true
        }
        
    }
    
    //条用系统的init
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //准备界面
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //开启按钮的动画
    func startAnimation()
    {
         //显示按钮
        btn.hidden = false
        
        //设置没有
        btn.transform = CGAffineTransformMakeScale(0, 0)
        
        //使用UIView动画效果
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.btn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("开启动画完成")
        }
    }
    
    
    func prepareUI()
    {
        //添加内容
         contentView.addSubview(backGroundImage)
        contentView.addSubview(btn)
        
        //清除约束
        backGroundImage.translatesAutoresizingMaskIntoConstraints = false
        btn.translatesAutoresizingMaskIntoConstraints = false
        
        //添加背景的约束
        contentView.addConstraint(NSLayoutConstraint(item: backGroundImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backGroundImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backGroundImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backGroundImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        //添加按钮的约束
        contentView.addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: btn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        
    }
    
     func startBtnClick()
    {
           //获得系统的appDelegate来进行切换,必须强制转换成系统的delegate
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
    }
    
    //懒加载
    lazy var backGroundImage:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    //按钮的懒加载
    lazy var btn:UIButton = {
         let loginBtn = UIButton()
        //设置背景图片
        loginBtn .setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        loginBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        loginBtn.setTitle("开始体验", forState: UIControlState.Normal)
        
        //给开始体验按钮设置事件，用于切换控制器
        loginBtn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
         return loginBtn
    }()
}















